package cn.gok.service.impl;

import cn.gok.bo.DormitoryBo;
import cn.gok.bo.StudentInfoBo;
import cn.gok.dao.ManagerDao;
import cn.gok.entity.TDormitory;
import cn.gok.entity.TDormitoryPositon;
import cn.gok.entity.TStudent;
import cn.gok.entity.TStudentDormitory;

import cn.gok.service.ManagerService;
import cn.gok.util.Const;
import cn.gok.util.R;
import cn.gok.util.Sqlsessionutil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ManagerServiceImpl  implements ManagerService {
    private SqlSession sqlSession;
    private ManagerDao managerDao;

    public ManagerServiceImpl(){
        sqlSession = Sqlsessionutil.getSqlSessionFactory().openSession(true);
        managerDao = sqlSession.getMapper(ManagerDao.class);
    }



    @Override
    public R queryStudent(String realName, String gender, String schoolName, String startTime, String  denTime,int currentPage, int pageSize) {
        List<StudentInfoBo> studentInfoBos = managerDao.queryStudent(realName, gender, schoolName, startTime, denTime);
        System.out.println("1111111111111111"+studentInfoBos);
        //开启分页
        PageHelper.startPage(currentPage, pageSize);

        PageInfo pageInfo = new PageInfo(studentInfoBos);
        /*
        respData:{

        },
        datas{

        }

         */
        R r = new R<>();
        r.put("result",true);
        r.put("msg","");

        Map<String ,Object> pagination=new HashMap<>();
        pagination.put("currentPage",currentPage);
        pagination.put("pageSize",pageSize);
        pagination.put("total",pageInfo.getTotal());

        r.put("pagination",pagination);
        r.put("datas",studentInfoBos);
        return r;

    }

    @Override
    public R addStudent(String realName, String stuName, String password, String gender, int age, String schoolName, String phoneNum) {
        int i = managerDao.addStudent(realName, stuName, password, gender, age, schoolName, phoneNum);
        if (i>0){
            return R.success();
        }
        else {
            return R.fail();
        }

    }

    @Override
    public R addDormitory(String position, String apartmentName, String floor, String doorNum, String max, String type, String state, int managerId) {
        int i = managerDao.addDormitory(position, apartmentName, floor, doorNum, max, type, state, managerId);
        if (i>0){
          return   R.success();
        }
        else {
            return R.fail();
        }
    }

    @Override
    public R queryPosition() {
        List<String> list = managerDao.queryPosition();
        return R.create(list);
    }

    @Override
    public R queryApartmentName() {
        List<String> list = managerDao.queryApartmentName();
        return R.create(list);
    }

    @Override
    public int addStudents(List<TStudent> student) {
        int i = managerDao.addStudents(student);
        return i;
    }

    @Override
    public R updateDormitory(String doorNum, String type, String max, String apartmentName, String position, String oldDoorNum) {
        int i = managerDao.updateDormitory(doorNum, type, max, apartmentName, position, oldDoorNum);
        if (i>0){
            return R.success();
        }
        else {
            return R.fail();
        }
    }

    @Override
    public R queryFloor(String position, String apartmentName) {
        List<String> list = managerDao.queryFloor(position, apartmentName);
        return R.create(list);
    }

    @Override
    public R queryDormitory(String apartmentName, String floor, String type, String state,int currentPage, int pageSize) {
        List<TDormitory> tDormitories = managerDao.queryDormitory(apartmentName, floor, type, state);
        System.out.println("1111111111111111"+tDormitories);
        //开启分页
        PageHelper.startPage(currentPage, pageSize);

        PageInfo pageInfo = new PageInfo(tDormitories);
        /*
        respData:{

        },
        datas{

        }

         */
        R r = new R<>();
        r.put("result",true);
        r.put("msg","");

        Map<String ,Object> pagination=new HashMap<>();
        pagination.put("currentPage",currentPage);
        pagination.put("pageSize",pageSize);
        pagination.put("total",pageInfo.getTotal());

        r.put("pagination",pagination);
        r.put("datas",tDormitories);
        return r;
    }

    @Override
    public List<TDormitory> queryAllDoorNumByDormPosId(String  id, int state) {
        List<TDormitory> dormitories = managerDao.queryAllDoorNumByDormPosId(id,state);
        return dormitories;
    }

    @Override
    public TDormitory queryDormitoryByDoor(int doorNum) {
        TDormitory dormitory = managerDao.queryDormitoryByDoor(doorNum);
        return dormitory;
    }

    @Override
    public R reduceSurplusNumByDoor(int doorNum, int state) {
        int i = managerDao.reduceSurplusNumByDoor(doorNum,state);
        if (i>0){
            return R.success(Const.SUCCESS_OPERATION_MSG);
        }
        return R.fail(Const.FAIL_OPERATION_MSG);
    }

    @Override
    public R querySurplusNumByDoor(int doorNum) {
        int i = managerDao.querySurplusNumByDoor(doorNum);
        if (i>0){
            return R.success(Const.SUCCESS_OPERATION_MSG);
        }
        return R.fail(Const.FAIL_OPERATION_MSG);
    }

    @Override
    public TStudentDormitory queryStuDormitoryByStuId(int id) {
        TStudentDormitory stuDormitory = managerDao.queryStuDormitoryByStuId(id);
        return stuDormitory;
    }

    @Override
    public R studentCheckIn(int stuId, int dormId) {
        int i = managerDao.studentCheckIn(stuId, dormId);
        if (i>0){
            R success = R.success();
            return success;
        }
        return R.fail(Const.FAIL_OPERATION_MSG);
    }

    @Override
    public TStudent queryStudentById(String stuId) {

        TStudent student = managerDao.queryStudentById(stuId);
        if (student!=null){

            return student;
        }
        else {
            return null;
        }
    }

    @Override
    public List<TDormitoryPositon> queryAllDormitoryPosition() {
        List<TDormitoryPositon> tDormitoryPositons = managerDao.queryAllDormitoryPosition();
        return tDormitoryPositons;
    }

    @Override
    public R out(String stuId) {
        int out = managerDao.out(stuId);
        if (out>0){
            return R.success();
        }
        return R.fail();
    }

    @Override
    public R addSurplus(String stuId) {
        int i = managerDao.addSurplus(stuId);
        if (i>0){
            return R.success();
        }
        return R.fail();
    }

    @Override
    public R queryStudentLive(String stuId) {
        TStudent student = managerDao.queryStudentLive(stuId);
        if (student!=null){
            return R.success();
        }
        return R.fail();
    }

    @Override
    public R queryAll() {
        List<TDormitory> tDormitories = managerDao.queryAll();
         int allRoom = 0;
         int allBed=0;
         int people=0;
         int man=0;
         int woman=0;
         int manLeft=0;
         int womanLeft=0;
         int nullLeft=0;
         int towLeft=0;
         int fourLeft=0;
         int sixLeft=0;
       for (TDormitory tDormitory:tDormitories){
           allBed=allBed+tDormitory.getType();
           allRoom=allRoom+1;
           people=people+tDormitory.getMaxNum()-tDormitory.getSurplusNum();
           if (tDormitory.getState()==1){
               man=man+tDormitory.getMaxNum()-tDormitory.getSurplusNum();
               manLeft=manLeft+tDormitory.getSurplusNum();
           }
           else if (tDormitory.getState()==2){
            woman=woman+tDormitory.getMaxNum()-tDormitory.getSurplusNum();
            womanLeft=womanLeft+tDormitory.getSurplusNum();
           }
           else if(tDormitory.getState()==0){
               nullLeft=nullLeft+tDormitory.getSurplusNum();
           }
           else if (tDormitory.getType()==2){
               towLeft=towLeft+tDormitory.getSurplusNum();
           }
           else if (tDormitory.getType()==4){
               fourLeft=fourLeft+tDormitory.getSurplusNum();
           }
           else if (tDormitory.getType()==6){
               sixLeft=sixLeft+tDormitory.getSurplusNum();
           }
       }
       DormitoryBo dormitoryBo=new DormitoryBo();
       dormitoryBo.setAllRoom(allRoom);
       dormitoryBo.setAllBed(allBed);
       dormitoryBo.setPeople(people);
       dormitoryBo.setMan(man);
       dormitoryBo.setManLeft(manLeft);
       dormitoryBo.setWoman(woman);
       dormitoryBo.setWomanLeft(womanLeft);
       dormitoryBo.setNullLeft(nullLeft);
       dormitoryBo.setTowLeft(towLeft);
       dormitoryBo.setFourLeft(fourLeft);
       dormitoryBo.setSixLeft(sixLeft);
        return R.create(dormitoryBo);
    }

}
